package com.aoip.telemetry.telemetry.configureui.business.analysis.element.controller;

import com.aoip.telemetry.telemetry.configureui.business.analysis.element.service.ElementService;
import com.aoip.telemetry.telemetry.configureui.common.annotation.Log;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo;
import com.aoip.telemetry.telemetry.configureui.common.enums.BusinessType;
import com.aoip.telemetry.telemetry.configureui.common.utils.poi.ExcelUtil;
import com.aoip.telemetry.telemetry.entity.element.ElementConfigEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 元素指标模块
 *
 * @author chenbai
 * @version 1.0
 * @date 2021/12/28 14:12
 */
@RestController
public class ElementController extends BaseController {


    @Resource
    private ElementService service;

    /**
     * 获取解析元素配置分页列表
     *
     * @param entity 单个元素
     * @return com.aoip.telemetry.telemetry.configureui.common.core.list.TableDataInfo
     * @author chenbai
     * @date 2021/12/27
     **/
    @GetMapping("/v1/GET/analysis/element/page")
    public TableDataInfo page(ElementConfigEntity entity) {
        startPage();
        List<ElementConfigEntity> list = service.list(entity);
        return getDataTable(list);
    }

    /**
     * 解析组元素列表
     *
     * @param entity 单个元素
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2021/12/28
     **/
    @GetMapping("/v1/GET/analysis/element/list")
    public AjaxResult list(ElementConfigEntity entity) {
        List<ElementConfigEntity> list = service.list(entity);
        return AjaxResult.success(list);
    }

    /**
     * 获取单个解析元素详情
     *
     * @param id 数据id
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2021/12/27
     **/
    @GetMapping("/v1/GET/analysis/element/{id}")
    public AjaxResult station(@PathVariable String id) {
        return AjaxResult.success(service.element(id));
    }

    /**
     * 新增解析元素
     */
    @Log(title = "解析元素", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/analysis/element")
    public AjaxResult add(@Validated @RequestBody ElementConfigEntity entity) {
        String id = entity.getId();
        ElementConfigEntity element = service.element(id);
        if (element != null) {
            return AjaxResult.error("新增解析元素'" + element.getFieldName() + "'失败，该解析元素已存在。");
        }
        return toAjax(service.insert(entity));
    }

    /**
     * 修改解析元素
     */
    @Log(title = "解析元素", businessType = BusinessType.UPDATE)
    @PutMapping("/v1/PUT/analysis/element")
    public AjaxResult edit(@Validated @RequestBody ElementConfigEntity entity) {
        return toAjax(service.update(entity));
    }

    /**
     * 初始化测站缓存数据到redis中，数据格式（id, element）,（要素数据Id，要素实体）
     */
    @Log(title = "初始化配置数据到redis", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/analysis/element/cache")
    public AjaxResult initCache() {
        service.initCache();
        return AjaxResult.success();
    }

    /**
     * 删除解析元素
     */
    @Log(title = "解析元素", businessType = BusinessType.DELETE)
    @DeleteMapping("/v1/DELETE/analysis/element/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        service.delete(ids);
        return success();
    }

    @Log(title = "解析元素", businessType = BusinessType.EXPORT)
    @PostMapping("/v1/POST/analysis/element/export")
    public void export(HttpServletResponse response, ElementConfigEntity entity) {
        List<ElementConfigEntity> list = service.list(entity);
        ExcelUtil<ElementConfigEntity> util = new ExcelUtil<>(ElementConfigEntity.class);
        util.exportExcel(response, list, "解析元素数据");
    }
}
